<?php
/**
 * Nadëb (Makú-Nadëb)
 * 
 * @author     Mateus Martins <mateusweb@gmail.com>
 * @copyright  Copyright 2010 mateusweb.com.br
 * @license    http://www.gnu.org/licenses/gpl.html | GPL
 * @package    Nadeb
 * @version    1.0.0
 */


/**
 * Class Nadeb_Report_Body
 * Monta o corpo da tabela do data grid
 * 
 * @category   Nadeb
 * @package    Nadeb_Report_Body
 * @copyright  Copyright 2010 mateusweb.com.br
 * @license    http://www.gnu.org/licenses/gpl.html | GPL
 */
abstract class Nadeb_Report_Body
{
	public static function get_xhtml($_bodyConfigs)
	{
		$params      = $_bodyConfigs["params"];
		$ar_columns  = $_bodyConfigs["columns"];
		$data        = $_bodyConfigs["data"];
		$readOnly    = $_bodyConfigs["read_only"];
		$tools       = $_bodyConfigs["tools"];
		$key         = $_bodyConfigs["key"];
		
		$gd = "\t\t\t<tbody>\n";
		if(!$data)
		{
			$gd .= "\t\t\t\t<tr>\n\t\t\t\t\t<td class='blank_search' colspan='99'>Nenhum Registro encontrado</td>\n\t\t\t\t</tr>\n";
		}
		else
		{
			foreach($data as $keys => $array)
			{
				$i = 0;
				
				$gd .= "\t\t\t\t<tr class='move'>\n";
				
				if( !$readOnly )
				{
					$gd .= "\t\t\t\t\t<td class='td_checkbox'><input name='delete[]' type='checkbox' value='{$array[$key]}' /></td>\n";
				}
	
				foreach($ar_columns as $columns => $params)
				{
					if(isset($params["function"]))
					{
						$name = ucfirst( substr( $params["function"],0,strpos( $params["function"],'(' ) ) );
						/*
						 * Dicionario criado para redefinir os antigos nomes de funções
						 * ex. set() > Swap() 
						 */
						
						$dictionary = array(
							"Set"      => "Swap",
							"Phpimage" => "Imagecrop",
						);
						if( isset( $dictionary[$name] ) )
						{
							$name = $dictionary[$name];
						}
						else
						{
							$name = $name;
						}
						
						$objName = ucfirst( $name );
						$obj = Nadeb_Report_ShowData::get_instance($objName);
						$obj->set_id( $array[$key] );
						$obj->set_string( $array[$columns] );
						$obj->set_params( $params );
						
						$string = $obj->get_xhtml();
					}
					else
					{
						$obj = Nadeb_Report_ShowData::get_instance("String");
						$obj->set_id( $array[$key] );
						$obj->set_string( $columns );
						$obj->set_array( $array );
						
						$string = $obj->get_xhtml();
					}
					$gd .= "\t\t\t\t\t<td class='col_". $i++ . "'>". $string ."</td>\n";
				}
				
				if( $tools )
				{
					foreach($tools as $columns => $val)
					{
						if( isset($params['rel']) ) $val = str_replace("/id","/rel/{$params['rel']}/id",$val);
						$gd .= "\t\t\t\t\t<td class='edit_$columns'><a href=\"/{$val}{$array[$key]}\">{$columns}</a></td>\n";
					}
				}	
				$gd .= "\t\t\t\t</tr>\n";
			}
		}
		$gd .= "\t\t\t</tbody>\n";
		
		return $gd;
	}
}
